import CTYPE from "./CTYPE";
import NProgress from 'nprogress';
import React from 'react';
import ReactDOM from 'react-dom';
// import DialogQRCode from "../components/common/DialogQRCode";
// import {Recharge} from '../components/user/Recharge'
// import {Renew} from "../components/user/Renew";
// import {Rename} from "../components/user/Rename";
import {Reallot} from "../components/signed/allot";
import {ReallotSell} from "../components/apply/allotSell";
import {StaffDetail} from "../components/staffMange/StaffDetail";


let Utils = (function () {

    let _setCurrentPage = (key, pageno) => {
        sessionStorage.setItem(key, pageno);
    };

    let _getCurrentPage = (key) => {
        return sessionStorage.getItem(key) ? parseInt(sessionStorage.getItem(key)) : 1
    };

    let _setTabIndex = (key, index) => {
        sessionStorage.setItem(key, index);
    };

    let _getTabIndex = (tabKey) => {
        return sessionStorage.getItem(tabKey) ? parseInt(sessionStorage.getItem(tabKey)) : 0
    };

    let _setTabKey = (key, v) => {
        sessionStorage.setItem(key, v);
    };

    let _getTabKey = (tabKey) => {
        return sessionStorage.getItem(tabKey);
    };

    let nProgress = (() => {
        NProgress.configure({showSpinner: false});
        return {
            start: () => {
                NProgress.start();
            },
            done: () => {
                NProgress.done();
            },
        }
    })();

    let qrcode = (() => {

        let show = (url, avatar, title) => {
            let div = document.createElement('div');
            document.body.appendChild(div);
            ReactDOM.render(<DialogQRCode url={url} avatar={avatar} title={title}/>, div);
        };

        return {show}
    })();


    let common = (() => {
        let closeModalContainer = (id_div) => {
            let e = document.getElementById(id_div);
            if (e) {
                document.body.removeChild(e);
            }
        };

        let createModalContainer = (id_div) => {
            //强制清理同名div，render会重复创建modal
            closeModalContainer(id_div);
            let div = document.createElement('div');
            div.setAttribute('id', id_div);
            document.body.appendChild(div);
            return div;
        };

        return {
            closeModalContainer,
            createModalContainer
        }
    })();


    let adm = (() => {

        let avatar = (img) => {
            return img ? img : require('../assets/images/common/logo_square.png');
        };

        return {avatar}

    })();

    let user = (() => {
        let currentPageKey = 'key-user-pageno';

        let setCurrentPage = (pageno) => {
            _setCurrentPage(currentPageKey, pageno);
        };

        let getCurrentPage = () => {
            return _getCurrentPage(currentPageKey);
        };

        let recharge = (user, rechargeSucc) => {
            let div = document.createElement('div');
            document.body.appendChild(div);
            ReactDOM.render(<Recharge user={user} rechargeSucc={rechargeSucc}/>, div);
        };

        let renew = (user, renewSucc) => {
            let div = document.createElement('div');
            document.body.appendChild(div);
            ReactDOM.render(<Renew user={user} renewSucc={renewSucc}/>, div);
        };

        let rename = (user, replaceUser) => {
            let div = document.createElement('div');
            document.body.appendChild(div);
            ReactDOM.render(<Rename user={user} replaceUser={replaceUser}/>, div);
        };

        // 添加分配工匠modle
        let reallot = (user, realloter) => {
            let div = document.createElement('div');
            document.body.appendChild(div);
            ReactDOM.render(<Reallot user={user} realloter={realloter}/>, div);
        };

        // 添加分配销售modle
        let reallot2 = (user, realloter) => {
            let div = document.createElement('div');
            document.body.appendChild(div);
            ReactDOM.render(<ReallotSell user={user} realloter={realloter}/>, div);
        };

        // 添加员工详细信息模板
        let staffDetail = (user) => {
            let div = document.createElement('div');
            document.body.appendChild(div);
            ReactDOM.render(<StaffDetail user={user}/>, div);
        };


        return {setCurrentPage, getCurrentPage, recharge, renew, rename}

    })();

    let odm = (() => {
        let currentPageKey = 'key-odm-pageno';

        let setCurrentPage = (pageno) => {
            _setCurrentPage(currentPageKey, pageno);
        };

        let getCurrentPage = () => {
            return _getCurrentPage(currentPageKey);
        };

        let logo = (src, flag) => {
            if (src) {
                return src;
            } else {
                switch (flag) {
                    case 'logo1':
                        return require('../assets/images/common/logo_login.png');
                    case 'logo2':
                        return require('../assets/images/common/logo.png');
                    case 'logo3':
                        return require('../assets/images/common/logo_square.png');
                }
            }
        };

        return {setCurrentPage, getCurrentPage, logo}

    })();

    let admin = (() => {
        let currentPageKey = 'key-admin-pageno';

        let setCurrentPage = (pageno) => {
            _setCurrentPage(currentPageKey, pageno);
        };

        let getCurrentPage = () => {
            return _getCurrentPage(currentPageKey);
        };

        return {setCurrentPage, getCurrentPage}

    })();


    let num = (() => {

        let formatPrice = value => {
            value += '';
            const list = value.split('.');
            const prefix = list[0].charAt(0) === '-' ? '-' : '';
            let num = prefix ? list[0].slice(1) : list[0];
            let result = '';
            while (num.length > 3) {
                result = `,${num.slice(-3)}${result}`;
                num = num.slice(0, num.length - 3);
            }
            if (num) {
                result = num + result;
            }
            return `¥ ${prefix}${result}${list[1] ? `.${list[1]}` : ''}`;
        };

        return {
            formatPrice
        }

    })();

    let pager = (() => {
        let convertOffset2Page = (result) => {

            let pageSize = result.limit || CTYPE.pagination.pageSize;
            let total = result.total;
            let offset = result.offset || 0;
            let current = offset / pageSize + 1;

            return {
                current: current,
                total: total,
                pageSize: pageSize
            }
        };

        let convertPage2Offset = (pager) => {
            let current = pager.current;
            if (isNaN(current) || current === 0) {
                current = 1;
            }
            return pager.pageSize * (current - 1);
        };
        return {convertOffset2Page, convertPage2Offset}

    })();


    return {
        common, adm, user, odm,
        num,
        pager,
        nProgress,
        admin, qrcode,
    };

})
();

export default Utils;
